/*
 *
 *jsBridge
 */

// 设置JSBridge
const setupWebViewJavascriptBridge = callback => {
  // 注册事件监听
  if (window.WebViewJavascriptBridge) {
    return callback(window.WebViewJavascriptBridge);
  } else {
    document.addEventListener('WebViewJavascriptBridgeReady', () => {
      callback(window.WebViewJavascriptBridge);
    }, false);
  }

  if (window.WVJBCallbacks) {
    return window.WVJBCallbacks.push(callback);
  }
  window.WVJBCallbacks = [callback];
  let WVJBIframe = document.createElement('iframe');
  WVJBIframe.style.display = 'none';
  WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
  document.documentElement.appendChild(WVJBIframe);
  setTimeout(function () {
    document.documentElement.removeChild(WVJBIframe);
  }, 0);
};

export default {
  JsBackCurPage (param, callback) {
    if (typeof param === 'function') {
      callback = param;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsBackCurPage', { 'action': 'JsBackCurPage', 'param': param || '无参数' }, function(responseData) {
        callback && callback(responseData);
      });
    });
  },
  // 绑定手机号
  JsGoBindPhone(param, callback) {
    if (typeof param === 'function') {
      callback = param;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsGoBindPhone', { 'action': 'JsGoBindPhone', 'param': param || '无参数' }, function(responseData) {
        callback && callback(responseData);
      });
    });
  },
  // 绑定微信
  JsGoBindWeChat(param, callback) {
    if (typeof param === 'function') {
      callback = param;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsGoBindWeChat', { 'action': 'JsGoBindWeChat', 'param': param || '无参数' }, function(responseData) {
        callback && callback(responseData);
      });
    });
  },
  // 中英文切换
  JsChangeLanguage(flag, callback) {
    if (typeof flag === 'function') {
      callback = flag;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsChangeLanguage', { 'action': 'JsChangeLanguage', 'param': flag }, function(responseData) {
        callback && callback(responseData);
      });
    });
  },
  // 获取登录信息
  JsGetLoginInfo(param, callback) {
    if (typeof param === 'function') {
      callback = param;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsGetLoginInfo', { 'action': 'JsGetLoginInfo', 'param': param || '无参数' }, function(responseData) {
        var _res = JSON.parse(responseData);
        console.log(_res);
        callback && callback(_res);
      });
    });
  },
  // 点击分享弹窗
  JsCallShareDialog(param, callback) {
    if (typeof param === 'function') {
      callback = param;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsCallShareDialog', { 'action': 'JsCallShareDialog', 'param': param || '无参数' }, function(responseData) {
        callback && callback(responseData);
      });
    });
  },
  // 退出登录
  JsGoToExitLogin(param, callback) {
    if (typeof param === 'function') {
      callback = param;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsGoToExitLogin', { 'action': 'JsGoToExitLogin', 'param': param || '无参数' }, function(responseData) {
        callback && callback(responseData);
      });
    });
  },
  // 点击分享弹窗
  JsCallStartShare(flag, callback) {
    if (typeof param === 'function') {
      callback = flag;
    }
    var options = { shareFlag: flag, shareTitle: '分享标题', shareText: '分享的内容哎嘿嘿都会好的', shareImageUrl: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3625344829,3079500447&fm=26&gp=0.jpg', shareUrl: 'https://www.baidu.com/' };
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsCallStartShare', { 'action': 'JsCallStartShare', 'param': options || '无参数' }, function(responseData) {
        callback && callback(responseData);
      });
    });
  },
  // IOS调相机
  JsGetPhoto(param, callback) {
    if (typeof param === 'function') {
      callback = param;
    }
    setupWebViewJavascriptBridge((bridge) => {
      bridge.callHandler('JsGetPhoto', { 'action': 'JsGetPhoto', 'param': param || '无参数' }, function(responseData) {
        callback && callback(responseData);
      });
    });
  }
};
